package com.pdf.editor.util;

import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;

public class DecryptPDF {

    public static final DecryptPDF instance = new DecryptPDF();

    public String decrypt(File file, String password) {
        String result = null;
        try (PDDocument document = PDDocument.load(file, password)) {
            if (document.isEncrypted()) {
                String filePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 3);

                document.setAllSecurityToBeRemoved(true);
                document.save(filePath + "_" + System.currentTimeMillis() + ".pdf");
            } else {
                result = "文件未设置密码";
            }
        } catch (Exception e) {
            result = "解密失败: " + e.getMessage();
        }

        return result;
    }
}
